package com.ttc.group2.videochat.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.quickblox.users.model.QBUser;
import com.ttc.group2.videochat.R;
import com.ttc.group2.videochat.utils.DialogUtils;
import com.ttc.group2.videochat.view.TitleBar;

public class ChatActivity extends BaseActivity implements OnClickListener {
	private ImageView imgBack, imgCancel;
	private TitleBar title, titleLand;
	private ViewStub conversion_empty;
	private ListView lvConversion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_conversation);

		// custom ActionBar
		customActionBar();
		
		initView();
	}

	private void customActionBar() {
		mActionbar = getSupportActionBar();
		mActionbar.setDisplayShowHomeEnabled(false);
		mActionbar.setDisplayShowTitleEnabled(false);
		mActionbar.setSplitBackgroundDrawable(new ColorDrawable(getResources()
				.getColor(R.color.videochat_blue)));
		LayoutInflater inflater = LayoutInflater.from(this);
		View customView = inflater.inflate(R.layout.custom_actionbar, null);
		imgBack = (ImageView) customView.findViewById(R.id.custom_action_back);
		imgCancel = (ImageView) customView
				.findViewById(R.id.custom_action_cancel);
		title = (TitleBar) customView.findViewById(R.id.custom_title);
		titleLand = (TitleBar) customView
				.findViewById(R.id.custom_title_landscape);
		title.title.setText("People name");

		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			titleLand.setVisibility(View.VISIBLE);
			title.setVisibility(View.GONE);
			titleLand.title.setText("People name");
		}

		imgBack.setOnClickListener(this);
		imgCancel.setOnClickListener(this);

		mActionbar.setCustomView(customView);
		mActionbar.setDisplayShowCustomEnabled(true);
	}

	private void initView() {
		conversion_empty = (ViewStub) findViewById(R.id.conversation_view_stub);
		lvConversion = (ListView) findViewById(R.id.conversation_list);
		conversion_empty.setVisibility(View.VISIBLE);
		lvConversion.setEmptyView(conversion_empty);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.custom_action_back:
			finish();
			break;
		case R.id.custom_action_cancel:
			finish();
		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_view_chat, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_video_call:
//			DialogUtils.showToast(this, "Video call", Toast.LENGTH_SHORT);
			Intent i=getIntent();
			QBUser u=(QBUser)i.getSerializableExtra("aa");
			Intent ii=new Intent(this, VideoCallActivity.class);
			ii.putExtra("aa", u);
			startActivity(ii);
			return true;
		case R.id.menu_view_profile:
			DialogUtils.showToast(this, "View profile", Toast.LENGTH_SHORT);
			return true;
		case R.id.settings:
			DialogUtils.showToast(this, "Settings", Toast.LENGTH_SHORT);
			return true;
		case R.id.menu_item_sign_out:
			DialogUtils.showToast(this, "Sign out", Toast.LENGTH_SHORT);
			return true;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			toggleActionBar();
		}
		return super.onTouchEvent(event);
	}

	private void toggleActionBar() {
		if (mActionbar != null) {
			if (mActionbar.isShowing()) {
				mActionbar.hide();
			} else {
				mActionbar.show();
			}
		}
	}
}
